<template>
    <div class="SearchResult" @scroll="handleScroll">
        <div class="tops">搜索 <span>{{ searchInput }}</span></div>
        <div class="content">
            <div class="tabs">
                <span @click="getMusic(1018)" :class="tabs === 1018 ? 'span' : ''">综合</span>
                <span @click="getMusic(1)" :class="tabs === 1 ? 'span' : ''">歌曲</span>
                <span @click="getMusic(100)" :class="tabs === 100 ? 'span' : ''">音乐人</span>
                <span @click="getMusic(10)" :class="tabs === 10 ? 'span' : ''">专辑</span>
                <span @click="getMusic(1000)" :class="tabs === 1000 ? 'span' : ''">歌单</span>
                <span @click="getMusic(1004)" :class="tabs === 1004 ? 'span' : ''">MV</span>
            </div>
            <div class="comprehensive" v-if="!loading">
                <div v-if="tabs === 1018" class="possibleSinger">
                    <p>你可能想搜</p>
                    <div>
                        <img :src="artists[0]?.picUrl">
                        <div>
                            <p>{{ artists[0]?.name }}</p>
                            <div style="display: flex; align-items: center;">
                                <span>{{ artists[0]?.mvSize }}万人关注·</span>
                                <span>{{ artists[0]?.albumSize }}首歌</span>
                            </div>
                        </div>
                    </div>
                </div>
                <div v-if="tabs === 1 || tabs === 1018" class="songList">
                    <p>歌曲</p>
                    <MusicList :data="songs" />
                </div>
                <div v-if="tabs === 1018 || tabs === 100" class="Musician">
                    <p>音乐人</p>
                    <div class="possibleSingers" v-for="item in artists" :key="item.id"
                        @click="() => router.push({ path: '/singer/Details', query: { id: item.id } })">
                        <img v-lazys="item?.picUrl">
                        <div>
                            <p>{{ item?.name }}</p>
                            <div style="display: flex; align-items: center;">
                                <span>{{ item?.mvSize }}万人关注·</span>
                                <span>{{ item?.albumSize }}首歌</span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="album" v-if="tabs === 10">
                    <div class="albumTop">
                        <div>#</div>
                        <div style="width: 20vw;">标题</div>
                        <div style="width: 25vw;">作者</div>
                        <div style="width: 8vw;">时间</div>
                    </div>
                    <div class="albumItem" v-for="(item, index) in albums" :key="index" @click="goAblum($event, item.id)">
                        <div>{{ index + 1 }}</div>
                        <div class="title">
                            <img v-lazys="item.blurPicUrl">
                            <div>
                                {{ item.name }}
                                {{ item.alias.length != 0 ? (item.alias[0]) : '' }}
                            </div>
                        </div>
                        <div class="author" style="width: 25vw;">{{ item.artist.name }}</div>
                        <div style="width: 8vw;" class="time">
                            {{ getTimestampConversion(item.publishTime) }}
                        </div>
                    </div>
                </div>
                <div class="songSheet" v-if="tabs === 1000">
                    <div class="possibleSingers" v-for="item in playlists" @click="getHandleSongData(item)" :key="item.id">
                        <img v-lazys="item.coverImgUrl">
                        <div>
                            <p>{{ item.name }}</p>
                            <div style="display: flex; align-items: center;">
                                <span>创建者：{{ item.creator.nickname }} · </span>
                                <span>{{ item.trackCount }}首歌</span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="mv" v-if="tabs === 1004">
                    <div class="mvItem" v-for="item in mvs" @click="goMvPlay(item)" :key="item.id">
                        <img v-lazys="item.cover" alt="">
                        <div>{{ item.name }}</div>
                        <div>{{ item.artistName }}</div>
                    </div>
                </div>
            </div>
        </div>
        <div v-if="loader" class="logn">
            <div class="loader"></div>
        </div>
        <div style="position: absolute;top: 50vh;width: 75%;background: rgba(0,0,0,0.4);" v-loading="loading"
            element-loading-text="加载歌单列表中🍒...">
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch, toRefs } from 'vue';
import { useRouter } from 'vue-router';
import useCounterStore from '@/stores/counter'
import * as searchAPI from '@/api/search/index'
import MusicList from '@/components/musicList.vue'
const { searchInput, songSheetInfo } = toRefs(useCounterStore())//获取pinia中的数据
const { getTimestampConversion } = useCounterStore()//获取pinia中的方法
const router = useRouter()
const tabs: any = ref(null)
const limit = ref(20)//初始获取的数据量
const loader = ref(false)//触底是否显示loader
const albums = ref([] as Array<any>)//专辑列表
const artists = ref([] as Array<any>)//歌手列表
const songs = ref([] as Array<any>)//歌曲列表
const mvs = ref([] as Array<any>)//mv列表
const playlists = ref([] as Array<any>)//歌单列表
// const djRadios = ref([] as Array<any>)//电台列表
const loading = ref<boolean>(true)
onMounted(() => {
    getMusic(1018)
})
//触底加载数据
const handleScroll = (e: any) => {
    // 如果加载器为空
    if (!loader.value) {
        // 如果滚动条距离加上客户端高度大于等于滚动条距离加上滚动高度减去10
        if (e.target.scrollTop + e.target.clientHeight >= e.target.scrollHeight - 10) {
            // 如果标签页为1
            if (tabs.value === 1) {
                // 将加载器设置为true
                loader.value = true
                // 将限制器设置为限制器加上10
                limit.value += 10
                // 获取音乐
                getMusic(tabs.value)
            }
        }
    }

}

//根据关键字搜索内容
const getMusic = (type: number) => {
    // 如果加载器存在，将加载器设置为false
    if (loader.value) {
        loading.value = false
    }
    // 将tabs设置为type
    tabs.value = type
    // 如果type等于1，调用getCloudsearch方法，传入limit和keywords，将songs设置为返回的数据，将loader和loading设置为false
    if (type == 1) {
        searchAPI.getCloudsearch({ limit: limit.value, type, keywords: searchInput.value }).then(({ data }) => {
            songs.value = data.result.songs
            loader.value = false
            loading.value = false
        })
        // 否则，调用getSearch方法，传入limit和keywords，如果返回的code等于200，将res设置为返回的数据，根据type的值，将对应的值设置为返回的数据，将loader和loading设置为false
    } else {
        searchAPI.getSearch({ limit: limit.value, type, keywords: searchInput.value }).then(({ data }) => {
            if (data.code === 200) {
                const res = data.result
                // 如果type等于100，将artists设置为返回的数据
                if (type === 100) {
                    artists.value = res.artists
                    // 如果type等于10，将albums设置为返回的数据
                } else if (type === 10) {
                    albums.value = res.albums
                    // 如果type等于1000，将playlists设置为返回的数据
                } else if (type === 1000) {
                    playlists.value = res.playlists
                    // 如果type等于1004，将mvs设置为返回的数据
                } else if (type === 1004) {
                    mvs.value = res.mvs
                    // 如果type等于1018，将songs和artists设置为返回的数据
                } else if (type === 1018) {
                    songs.value = res.song.songs
                    artists.value = res.artist.artists
                }
            }
            // 设置loader和loading的值为false
            loader.value = false
            loading.value = false
        })
    }
}
//获取表单中的数据
const getHandleSongData = (item: any) => {
    songSheetInfo.value = item
    router.push('/home/songSheetInfo')
}
//跳转mv播放页
const goMvPlay = (item: any) => {
    router.push({ path: '/home/mvplay', query: item })
}

//跳转专辑详情页
function goAblum(event: any, id: number) {
    event.stopPropagation()//阻止冒泡
    router.push({ path: '/songs/allAlbums', query: { id } })
}

// 当搜索输入框发生变化时，调用getMusic函数，传入tabs的值
watch(searchInput, () => {
    getMusic(tabs.value)
})

</script>

<style lang="scss" scoped>
.SearchResult {
    width: 100%;
    height: 91%;
    margin-top: 8vh;
    padding-bottom: 20px;
    overflow: auto;

    .tops {
        color: #DCDCDC;
        font-weight: 500;
        font-size: 20px;
        margin: 20px 4vw;

        span {
            color: #00CB64;
            margin-left: 2vw;
        }
    }

    .content {
        .tabs {
            width: 35vw;
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-left: 4vw;

            span {
                cursor: pointer;
                padding-bottom: 10px;
                color: rgba(255, 255, 255, 0.3);
            }

            .span {
                border-bottom: 2px solid #fff;
            }
        }
    }

    .comprehensive {
        width: 90%;
        margin-top: 3.5vh;

        .possibleSinger {
            width: 100%;
            height: 15vh;
            padding: 10px;
            box-sizing: border-box;
            background: #1E1E1E;
            margin-left: 4vw;
            margin-top: 2vw;
            border-radius: 10px;

            p {
                font-size: 12px;
                color: #7C7C7C;
            }

            img {
                width: 4.5vw;
                height: 4.5vw;
                border-radius: 50px;
            }

            >div {
                display: flex;
                align-items: center;
                margin-top: 12px;

                >div {
                    margin-left: 10px;
                }

                span {
                    color: #6D6D6D;
                    font-size: 10px;
                }

                p {
                    color: #fff;
                    margin-bottom: 6px;
                }
            }
        }

        .songList {
            width: 100%;
            background: #1E1E1E;
            margin-left: 4vw;
            border-radius: 10px;
            margin-top: 10px;
            overflow: hidden;
            color: #7B7B7B;
            font-size: 12px;


            p {
                color: #fff;
                margin: 10px;
            }

            .item {
                display: flex;
                width: 100%;
                height: 34px;
                margin: 10px 0;
                align-items: center;

                >div {
                    white-space: nowrap;
                    overflow: hidden;
                    text-overflow: ellipsis;
                }

                >div:nth-child(1) {
                    width: 6vw;
                    text-align: center;
                }

                >div:nth-child(2) {
                    width: 18vw;
                    text-align: left;
                    padding-left: 20px;
                }

                >div:nth-child(3) {
                    width: 15vw;
                    text-align: left;
                }

                >div:nth-child(4) {
                    width: 20vw;
                    text-align: left;
                    padding-right: 4vw;

                }

                >div:nth-child(5) {
                    >div {
                        padding: 10px;
                    }

                    >div:hover {
                        background: rgba(255, 255, 255, 0.6);
                    }
                }
            }

            .cursor {
                cursor: pointer;
            }

            .cursor:hover {
                background: rgba(255, 255, 255, 0.2);
                border-radius: 4px;
            }
        }
    }

    .possibleSingers {
        width: 100%;
        height: 7vh;
        display: flex;
        align-items: center;
        cursor: pointer;
        padding: 5vh 10px;

        p {
            font-size: 12px;
            color: #7C7C7C;
        }

        img {
            width: 4vw;
            height: 4vw;
            border-radius: 50px;
        }

        >div {
            margin-left: 10px;

            span {
                color: #6D6D6D;
                font-size: 10px;
            }

            p {
                color: #fff;
                margin-bottom: 3px;
            }
        }
    }

    .Musician {
        width: 100%;
        margin-left: 4vw;
        background: #1E1E1E;
        border-radius: 10px;
        margin-top: 10px;
        padding-bottom: 10px;
        margin-top: 3vh;

        >p {
            padding: 10px;
            color: #fff;
        }
    }
}

.SearchResult::-webkit-scrollbar {
    display: none;
}

.logn {
    width: 100%;
    display: flex;
    justify-content: center;

    .loader {
        width: 15px;
        aspect-ratio: .75;
        --c: no-repeat linear-gradient(#ffffff 0 0);
        background:
            var(--c) 0% 50%,
            var(--c) 25% 50%,
            var(--c) 50% 50%,
            var(--c) 75% 50%,
            var(--c) 100% 50%;
        animation: l7 0.8s infinite linear alternate;
    }

    @keyframes l7 {
        0% {
            background-size: 10% 50%, 10% 50%, 10% 50%, 10% 50%, 10% 50%;
        }

        20% {
            background-size: 10% 20%, 10% 50%, 10% 70%, 10% 20%, 10% 50%;
        }

        40% {
            background-size: 10% 20%, 10% 50%, 10% 70%, 10% 50%, 10% 50%;
        }

        60% {
            background-size: 10% 50%, 10% 70%, 10% 50%, 10% 20%, 10% 80%;
        }

        80% {
            background-size: 10% 50%, 10% 70%, 10% 50%, 10% 70%, 10% 20%;
        }

        100% {
            background-size: 10% 50%, 10% 50%, 10% 50%, 10% 20%, 10% 40%;
        }
    }
}

.album {
    width: 100%;
    margin: 2vw 4vw;
    background: #1E1E1E;
    border-radius: 10px;
    padding-top: 10px;

    .albumTop {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;
        color: #DCDCDC;
        padding: 10px;
        font-size: 12px;
        box-sizing: border-box;

        >div:nth-child(1) {
            margin-left: 2vw;
            color: #7C7C7C;
            font-size: 14px;
        }
    }

    .selectAlbum {
        background: rgba(0, 0, 0, 0.5);
    }

    .albumItem {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;
        color: #fff;
        padding: 10px;
        box-sizing: border-box;
        cursor: pointer;

        >div:nth-child(1) {
            margin-left: 2vw;
            color: #7C7C7C;
            font-size: 14px;
        }

        img {
            width: 5vw;
            height: 5vw;
            border-radius: 6px;
            margin-right: 1vw;
        }

        .title {
            width: 20vw;
            display: flex;
            align-items: center;
            color: #DCDCDC;
            font-size: 14px;
        }

        .author,
        .time {
            font-size: 12px;
            color: #7C7C7C;
        }
    }

    .albumItem:hover {
        background: rgba(255, 255, 255, 0.2);
        border-radius: 4px;
    }
}

.songSheet {
    width: 100%;
    background: #1E1E1E;
    padding-top: 10px;
    margin-left: 4vw;
    border-radius: 10px;
    margin-top: 3vh;


    .possibleSingers {
        width: 100%;
        height: 10px;
        margin: 0 auto;
        padding: 4vh 10px;
    }

}

.possibleSingers:hover {
    background: rgba(255, 255, 255, 0.2);
    border-radius: 4px;
}

.mv {
    width: 100%;
    background: #1E1E1E;
    margin: 0 4vw;
    display: flex;
    flex-wrap: wrap;
    border-radius: 10px;

    .mvItem {
        width: 30%;
        margin: 1.2vw;
        overflow: hidden;
        cursor: pointer;
        /* margin-left: 4vw; */

        img {
            width: 100%;
            border-radius: 10px;
        }

        >div {
            margin-left: 1vw;
        }

        >div:nth-child(2) {
            margin: 4px 1vw;
            color: #fff;
            font-size: 14px;
        }

        >div:nth-child(3) {
            color: rgba(255, 255, 255, 0.2);
            font-size: 12px;
        }
    }
}
</style>